<template>
  <div>
    <!-- 查询 -->
    <div class="div0">
      <el-row :gutter="12">
        <el-col :span="5.5"
          >违章标题：<el-input
            v-model="Viotitel"
            placeholder="请输入违章标题："
            size="medium"
          ></el-input
        ></el-col>
        <el-col :span="5.5"
          >车牌号：<el-input
            v-model="VioNumber"
            placeholder="请输入车牌号"
            size="medium"
          ></el-input
        ></el-col>
        <el-col :span="1.5"
          ><el-button type="primary" size="medium" @click="getCara"
            >查询</el-button
          ></el-col
        >
      </el-row>

      <el-row :gutter="12">
        <el-col :span="1.5"
          ><el-button type="primary" size="medium" @click="OppenAdd"
            >新增</el-button
          ></el-col
        >
      </el-row>
    </div>

    <!-- 分页显示 -->
    <div class="div1">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="Viotitel" label="违章标题" width="150">
        </el-table-column>
        <el-table-column prop="VioNumber" label="车牌号" width="150">
        </el-table-column>
        <el-table-column prop="Viomiao" label="违章内容" width="150">
        </el-table-column>
        <el-table-column prop="Vioyuan" label="处罚结果" width="200">
        </el-table-column>
        <el-table-column prop="VioPeop" label="违章人" width="250">
        </el-table-column>
        <el-table-column prop="VioTimToo" label="违章日期" width="250">
        </el-table-column>
        <el-table-column prop="VioTxt" label="备注" width="250">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click="OppenLook(scope.row)" type="text" size="small"
              >查看</el-button
            >
            <el-button @click="OppenUp(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div style="float: right; margin-top: 20px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageindex"
          :page-sizes="[1, 3, 5, 10]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagetotal"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 添加 -->
    <div>
      <el-dialog title="新增违章记录" :visible.sync="dialogFormVisible">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章标题："
                :label-width="formLabelWidth"
                prop="Viotitel"
              >
                <el-input
                  v-model="ruleForm.Viotitel"
                  autocomplete="off"
                  placeholder="请输入违章标题"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="16">
            <el-col :span="4.5">
              <el-form-item
                label="车牌号："
                :label-width="formLabelWidth"
                prop="VioNumber"
              >
                <el-input
                  v-model="ruleForm.VioNumber"
                  autocomplete="off"
                  placeholder="请输入车牌号"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章人"
                :label-width="formLabelWidth"
                prop="VioPeop"
              >
                <el-input
                  v-model="ruleForm.VioPeop"
                  autocomplete="off"
                  placeholder="请输入违章人"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章日期："
                :label-width="formLabelWidth"
                prop="VioTim"
              >
                <el-date-picker
                  v-model="ruleForm.VioTim"
                  type="date"
                  autocomplete="off"
                  placeholder="请选择违章日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章内容："
                :label-width="formLabelWidth"
                prop="Viomiao"
              >
                <el-input
                  v-model="ruleForm.Viomiao"
                  autocomplete="off"
                  placeholder="请输入违章内容"
                  type="textarea"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="处罚结果："
                :label-width="formLabelWidth"
                prop="Vioyuan"
              >
                <el-input
                  v-model="ruleForm.Vioyuan"
                  autocomplete="off"
                  placeholder="请输入处罚结果"
                  type="textarea"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="备注：" :label-width="formLabelWidth">
                <el-input
                  v-model="ruleForm.VioTxt"
                  type="textarea"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForm('ruleForm')">取消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
        </div>
      </el-dialog>
    </div>

    <!-- 修改 -->
    <div>
      <el-dialog title="修改违章记录" :visible.sync="dialogFormVisibles">
        <el-form
          :model="ruleForms"
          :rules="rules"
          ref="ruleForms"
          class="demo-ruleForm"
        >
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章标题："
                :label-width="formLabelWidth"
                prop="Viotitel"
              >
                <el-input
                  v-model="ruleForms.Viotitel"
                  autocomplete="off"
                  placeholder="请输入违章标题"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="16">
            <el-col :span="4.5">
              <el-form-item
                label="车牌号："
                :label-width="formLabelWidth"
                prop="VioNumber"
              >
                <el-input
                  v-model="ruleForms.VioNumber"
                  autocomplete="off"
                  placeholder="请输入车牌号"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章人："
                :label-width="formLabelWidth"
                prop="VioPeop"
              >
                <el-input
                  v-model="ruleForms.VioPeop"
                  autocomplete="off"
                  placeholder="请输入违章人："
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章日期："
                :label-width="formLabelWidth"
                prop="VioTim"
              >
                <el-date-picker
                  v-model="ruleForms.VioTim"
                  type="date"
                  autocomplete="off"
                  placeholder="请选择违章日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章内容："
                :label-width="formLabelWidth"
                prop="Viomiao"
              >
                <el-input
                  v-model="ruleForms.Viomiao"
                  autocomplete="off"
                  placeholder="请输入违章内容"
                  type="textarea"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="处罚结果："
                :label-width="formLabelWidth"
                prop="Vioyuan"
              >
                <el-input
                  v-model="ruleForms.Vioyuan"
                  autocomplete="off"
                  placeholder="请输入处罚结果"
                  type="textarea"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="备注：" :label-width="formLabelWidth">
                <el-input
                  v-model="ruleForms.VioTxt"
                  autocomplete="off"
                  type="textarea"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForms('ruleForms')">取消</el-button>
          <el-button type="primary" @click="updateRep('ruleForms')"
            >立即修改</el-button
          >
        </div>
      </el-dialog>
    </div>

    <!-- 查看 -->
    <div>
      <el-dialog title="查看违章记录" :visible.sync="dialogFormVisibless">
        <el-form
          :model="ruleForms"
          :rules="rules"
          ref="ruleForms"
          class="demo-ruleForm"
        >
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章标题："
                :label-width="formLabelWidth"
                prop="Viotitel"
              >
                <el-input
                  v-model="ruleForms.Viotitel"
                  autocomplete="off"
                  readonly="readonly"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="16">
            <el-col :span="4.5">
              <el-form-item
                label="车牌号："
                :label-width="formLabelWidth"
                prop="VioNumber"
              >
                <el-input
                  v-model="ruleForms.VioNumber"
                  autocomplete="off"
                  readonly="readonly"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章人："
                :label-width="formLabelWidth"
                prop="VioPeop"
              >
                <el-input
                  v-model="ruleForms.VioPeop"
                  autocomplete="off"
                  readonly="readonly"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章日期："
                :label-width="formLabelWidth"
              >
                <el-date-picker
                  v-model="ruleForms.VioTim"
                  type="date"
                  autocomplete="off"
                  readonly="readonly"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="违章内容："
                :label-width="formLabelWidth"
                prop="Viomiao"
              >
                <el-input
                  v-model="ruleForms.Viomiao"
                  autocomplete="off"
                  type="textarea"
                  readonly="readonly"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="处罚结果："
                :label-width="formLabelWidth"
                prop="Vioyuan"
              >
                <el-input
                  v-model="ruleForms.Vioyuan"
                  autocomplete="off"
                  type="textarea"
                  readonly="readonly"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="备注：" :label-width="formLabelWidth">
                <el-input
                  v-model="ruleForms.VioTxt"
                  autocomplete="off"
                  type="textarea"
                  readonly="readonly"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getVio } from "@/api/Vehicles";
export default {
  data() {
    return {
      dialogFormVisible: false, //添加
      dialogFormVisibles: false, //修改
      dialogFormVisibless: false, //查看
      formLabelWidth: "120px",
      ruleForm: {},
      ruleForms: {},
      Viotitel: "",
      VioNumber: "",
      pageindex: 1,
      pagesize: 5,
      tableData: [],
      pagetotal: 0,
      rules: {
        Viotitel: [
          { required: true, message: "请输入违章标题", trigger: "change" },
        ],
        VioNumber: [
          { required: true, message: "请输入车牌号", trigger: "change" },
        ],
        VioPeop: [
          { required: true, message: "请输入违章人", trigger: "change" },
        ],
        VioTim: [
          { required: true, message: "请选择违章日期", trigger: "change" },
        ],
        Viomiao: [
          { required: true, message: "请输入违章内容", trigger: "change" },
        ],
        Vioyuan: [
          { required: true, message: "请输入处罚结果", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    getCara() {
      getVio(this.pageindex, this.pagesize, this.Viotitel, this.VioNumber).then(
        (res) => {
          this.tableData = res.data.PageData;
          this.pagetotal = res.data.PageTotal;
        }
      );
    },
    handleSizeChange(val) {
      this.pagesize = val;
      this.getCara();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.pageindex = val;
      this.getCara();
      console.log(`当前页: ${val}`);
    },
    //添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post(
              "https://localhost:44360/api/app/t-mS_Vio/vio-adds",
              this.ruleForm
            )
            .then((res) => {
              if (res.data == 1) {
                alert("添加成功!");
                this.dialogFormVisible = false;
                this.getCara();
              } else {
                alert("添加失败!");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //修改
    updateRep(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .put(
              "https://localhost:44360/api/app/t-mS_Vio/vioup",
              this.ruleForms
            )
            .then((res) => {
              if (res.data == 1) {
                alert("修改成功!");
                this.dialogFormVisibles = false;
                this.getCara();
              } else {
                alert("修改失败!");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    //关闭添加
    resetForm() {
      this.ruleForm = {};
      this.dialogFormVisible = false;
    },
    //关闭编辑和查看
    resetForms() {
      this.dialogFormVisibles = false;
      this.dialogFormVisibless = false;
    },
    //打开添加
    OppenAdd() {
      this.dialogFormVisible = true;
    },
    //打开编辑
    OppenUp(row) {
      this.ruleForms = row;
      this.dialogFormVisibles = true;
    },
    //打开查看
    OppenLook(row) {
      this.ruleForms = row;
      this.dialogFormVisibless = true;
    },
    //删除
    handleDelete(index, row) {
      if (confirm("确定删除此条数据吗?")) {
        this.$axios
          .post("https://localhost:44360/api/app/t-mS_Vio/delet-Vio/" + row.Id)
          .then((res) => {
            if (res.data == 1) {
              alert("删除成功!");
              this.getCara();
            }
          });
      }
    },
  },
  created() {
    this.getCara();
  },
  computed: {},
};
</script>
<style scoped>
.div0,
.div1 {
  margin-left: 1%;
  margin-top: 1%;
}
.el-row {
  margin-bottom: 20px;
}
.last-child {
  margin-bottom: 0;
}
.el-col {
  border-radius: 4px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.el-input {
  width: 250px;
}
</style>